#include"http.hpp"
//#include"DemoHttp.hpp"
//#include"DemoHttp2.hpp"
//#include"DemoHttp3.hpp"
//#include"DemoHttp4.hpp"
//#include"GitCode.hpp"
#define WWWROOT "./wwwroot"

//1.githttp myserver
//2.myhttp myserver
//3.myhttp gitserver
//4.githttp gitserver

std::string RequestStr(const HttpRequest &req) {
    std::stringstream ss;
    ss << req._method << " " << req._path<< " " << req._version<< "\r\n";
    for (auto &it : req._headcontents) {
    //for (auto &it : req._headers) {
        ss << it.first << ": " << it.second << "\r\n";
    }
    for (auto &it : req._params) {
        ss << it.first << ": " << it.second << "\r\n";
    }
    ss << "\r\n";
    ss << req._body;
    //DEBUG_LOG("%s",ss.str().c_str());
    return ss.str();
}
void Hello(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetContent(RequestStr(req), "text/plain");
    //sleep(15);//进行业务处理超时测试
}
void Login(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetContent(RequestStr(req), "text/plain");
}
void PutFile(const HttpRequest &req, HttpResponse *rsp) 
{
    //rsp->SetContent(RequestStr(req), "text/plain");

    std::string pathname = WWWROOT + req._path;
    DEBUG_LOG("%s",pathname.c_str());
    Util::WriteFile(pathname, req._body);
}
void DelFile(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetContent(RequestStr(req), "text/plain");
}
int main()
{
    HttpServer server(8085);
    server.SetThreadCount(3);
    server.SetBaseDir(WWWROOT);//设置静态资源根目录，告诉服务器有静态资源请求到来，需要到哪里去找资源文件
    server.Get("/hello", Hello);
    server.Post("/login", Login);
    server.Put("/1234.txt", PutFile);
    server.Delete("/1234.txt", DelFile);
    server.Listen();
    return 0;
}

// int main(){
//     // std::vector<std::string> vss;
//     // std::string src("get /bitejiuyeke/login?user=xiaoming&pass=123123 HTTP/1.1\r\ncontext:html\r\n\r\n");
//     // std::string sep("\r\n");
//     // int sz=Util::Split(src,sep,&vss);
//     // for(int i=0;i<sz;i++){
//     //     DEBUG_LOG("%s",vss[i].c_str());
//     //     if(vss[i].size()==0){
//     //         DEBUG_LOG("111");
//     //     }
//     // }

//     Context con;
//     // std::string req =   
//     // "GET /login?username=user123&password=mysecretpassword HTTP/1.1\r\n"  
//     // "Host: example.com\r\n"  
//     // "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3\r\n"  
//     // "Accept-Language: en-US,en;q=0.5\r\n"  
//     // "Accept-Encoding: gzip, deflate, br\r\n"  
//     // "Connection: keep-alive\r\n"  
//     // "\r\n";
//     std::string req =   
//         "POST /login HTTP/1.1\r\n"  
//         "Host: example.com\r\n"  
//         "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3\r\n"  
//         "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"  
//         "Accept-Language: en-US,en;q=0.5\r\n"  
//         "Accept-Encoding: gzip, deflate\r\n"  
//         "Content-Type: application/x-www-form-urlencoded\r\n"  
//         "Content-Length: 40\r\n"  
//         "Connection: keep-alive\r\n"  
//         "\r\n"  
//         "username=user123&password=mysecretpassword";  
//     Buffer buf;
//     buf.WriteStringAndPush(req);
//     con.HttpHandle(&buf);
//     DEBUG_LOG("HttpHandle done");
//     HttpRequest rq=con.GetRequest();
//     for(auto e:rq._params){
//         std::cout<<e.first<<":"<<e.second<<std::endl;
//     }
//     return 0;
// }